package org.jeecg.modules.wmhb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.constant.FillRuleConstant;
import org.jeecg.common.constant.UserConstant;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.SysDepartModel;
import org.jeecg.common.util.FillRuleUtil;
import org.jeecg.modules.wmhb.entity.*;
import org.jeecg.modules.wmhb.mapper.WmTaskbookMapper;
import org.jeecg.modules.wmhb.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 任务书表
 * @Author: jeecg-boot
 * @Date:   2024-09-06
 * @Version: V1.0
 */
@Service
public class WmTaskbookServiceImpl extends ServiceImpl<WmTaskbookMapper, WmTaskbook> implements IWmTaskbookService {
    @Autowired
    private IWmTaskFlowService wmTaskFlowService;
    @Autowired
    private IWmTaskService wmTaskService;
    @Autowired
    private IWmTaskbookFlowService wmTaskbookFlowService;
    @Autowired
    private IWmFlowDeptService wmFlowDeptService;
    @Autowired
    private IWmContractProductService wmContractProductService;
    @Autowired
    private IWmPurchaseTaskbookBomService wmPurchaseTaskbookBomService;
    @Autowired
    ISysBaseAPI sysBaseApi;
    @Autowired
    private IWmTaskLogService wmTaskLogService;
    @Autowired
    private IWmSkillChangeService wmSkillChangeService;
    @Autowired
    private IWmContractService wmContractService;
    @Autowired
    private IWmProjectService wmProjectService;

    @Override
    public boolean checkHasItem(String id) {
        int num = baseMapper.hasItemById(id);
        return num >0 ? true:false;
    };

    @Override
    public WmTaskbook findOneByBatchNo(String batchNo){
        LambdaQueryWrapper<WmTaskbook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmTaskbook::getBatchNo, batchNo).last("LIMIT 1");
        WmTaskbook wmTaskbook = getOne(queryWrapper);
        return wmTaskbook;
    };

    @Override
    public WmTaskbook transferTaskbook(WmTaskbook wmTaskbook, WmTaskbookFlow wmTaskbookFlow){
        WmTaskbook wmTaskbookNew = null; //初始化新任务书
        LambdaQueryWrapper<WmTaskbook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmTaskbook::getWmContractProductId, wmTaskbook.getWmContractProductId()).eq(WmTaskbook::getTaskbookType, wmTaskbookFlow.getTaskbookType()).last("LIMIT 1"); //同一产品同一事业部下的同一类型任务书只能有一个
        WmTaskbook wmTaskbookExist = getOne(queryWrapper);
        if(wmTaskbookExist == null) {
            List<WmFlowDept> wmFlowDeptList = wmFlowDeptService.selectByMainId(wmTaskbook.getWmTaskbookFlowId());

            //同一产品同一事业部下的同一类型任务书只能有一个
            wmTaskbookNew = new WmTaskbook();
            wmTaskbookNew.setWmTaskbookId(wmTaskbook.getId());
            wmTaskbookNew.setCode(wmTaskbook.getCode());
            wmTaskbookNew.setBuId(wmTaskbookFlow.getBuId());
            wmTaskbookNew.setWmTaskbookFlowId(wmTaskbookFlow.getId());
            wmTaskbookNew.setWmContractProductId(wmTaskbook.getWmContractProductId());
            wmTaskbookNew.setChargeBy(wmTaskbook.getChargeBy());
            wmTaskbookNew.setTenantId(wmTaskbook.getTenantId());
            wmTaskbookNew.setSysOrgCode(wmTaskbook.getSysOrgCode());
            wmTaskbookNew.setWmContractId(wmTaskbook.getWmContractId());
            wmTaskbookNew.setCurrentNodeIndex(0);
            wmTaskbookNew.setCurrentNodeId(wmFlowDeptList.get(0).getDeptId());
            if(wmTaskbookFlow.getTaskbookType().equals(UserConstant.TASKBOOK_TYPE_PRODUCE)) {
                wmTaskbookNew.setTaskbookType(UserConstant.TASKBOOK_TYPE_PRODUCE);
                wmTaskbookNew.setName(wmTaskbook.getName() + "_生产任务书");
            }
            else if(wmTaskbookFlow.getTaskbookType().equals(UserConstant.TASKBOOK_TYPE_PURCHASE)){
                wmTaskbookNew.setTaskbookType(UserConstant.TASKBOOK_TYPE_PURCHASE);
                wmTaskbookNew.setName(wmTaskbook.getName() + "_采购任务书");
            }
            save(wmTaskbookNew);
        }
        else{
            throw new RuntimeException("任务书已存在");
        }

        return wmTaskbookNew;
    };

    @Override
    public WmTaskbook selectByFolwAndContract(String wmTaskbookFlowId, String wmContractProductId){
        LambdaQueryWrapper<WmTaskbook> queryWrapperTaskbook = new LambdaQueryWrapper<>();
        queryWrapperTaskbook.eq(WmTaskbook::getWmTaskbookFlowId, wmTaskbookFlowId).eq(WmTaskbook::getWmContractProductId, wmContractProductId).last("LIMIT 1");
        WmTaskbook wmTaskbook = getOne(queryWrapperTaskbook);
        return wmTaskbook;
    };

    @Override
    public List<WmTaskbook> selectTaskbookWithTask(List<WmTaskbook> wmTaskbookList){
        List<WmTaskbook> wmTaskbookListNew = new ArrayList<>();
        for(WmTaskbook taskbook: wmTaskbookList){
            WmContract wmContract = wmContractService.getById(taskbook.getWmContractId());
            taskbook.setWmContract(wmContract);
            WmProject wmProject = wmProjectService.getById(wmContract.getWmProjectId());
            taskbook.setWmProject(wmProject);
            //1、通过任务书ID获取任务
            WmTask taskRoot = wmTaskService.getRootTaskByWmTaskbookIdAndVersion(taskbook.getId(), taskbook.getCurrentVersion());
            if(taskRoot == null){
                taskbook.setCanTransferProduce(false);
                taskbook.setCanTransferPurchase(false);
                taskbook.setCanTransferSkill(false);
                wmTaskbookListNew.add(taskbook);
                continue;
            }
            if(!StringUtils.isEmpty(taskRoot.getCurrentNodeId())) {
                SysDepartModel sysDepartModel = sysBaseApi.selectAllById(taskRoot.getCurrentNodeId());
                taskRoot.setCurrentNodeId_dictText(sysDepartModel.getDepartName());
            }
            taskRoot.setTaskbookStatus(taskbook.getStatus());
            taskRoot.setWmTaskbookFlowId(taskbook.getWmTaskbookFlowId());
            List<WmTask> wmTaskListChildren = queryTaskChildrenUntilStartPoint(taskRoot);
            taskRoot.setChildren(wmTaskListChildren);

            //2、获取任务流程时间明细
            List<WmTaskFlow> wmTaskFlowList = wmTaskFlowService.queryByTaskId(taskRoot.getId());
            taskRoot.setWmTaskFlowList(wmTaskFlowList);

            //3、任务列表添加到任务书
            List<WmTask> wmTaskList = new ArrayList<>();
            wmTaskList.add(taskRoot);
            taskbook.setChildren(wmTaskList);

            //4、判断能否转换为生产任务书
            if(taskbook.getTaskbookType().equals(UserConstant.TASKBOOK_TYPE_DESIGN) && taskbook.getIsTransferProduceTaskbook().equals(UserConstant.SYS_NO)){
                //是否有生产任务书，且没有转换过，则可以转换成生产任务书
                LambdaQueryWrapper<WmTaskbook> queryWrapperTaskbookProduce = new LambdaQueryWrapper<>();
                queryWrapperTaskbookProduce.eq(WmTaskbook::getWmTaskbookId, taskbook.getId()).last("LIMIT 1");
                WmTaskbook taskbookProduce = getOne(queryWrapperTaskbookProduce);
                if(taskbookProduce == null && taskbook.getStatus().equals(UserConstant.TASK_STATUS_PROCESS_APPROVED)){
                    taskbook.setCanTransferProduce(true);
                }
            }

            //5、判断能否转换为采购任务书
            if(taskbook.getTaskbookType().equals(UserConstant.TASKBOOK_TYPE_PROCESS) && taskbook.getIsTransferPurchaseTaskbook().equals(UserConstant.SYS_NO) && taskbook.getStatus().equals(UserConstant.TASK_STATUS_PROCESS_APPROVED)){
                taskbook.setCanTransferPurchase(true);
            }

            //6、判断采购任务的附加项是否确认完毕
            if(taskbook.getTaskbookType().equals(UserConstant.TASKBOOK_TYPE_PURCHASE)){
                taskRoot.setIsExtraConfirm(true);
                ////获取所有的采购BOM列表
                LambdaQueryWrapper<WmPurchaseTaskbookBom> queryWrapperTaskbookBom = new LambdaQueryWrapper<>();
                queryWrapperTaskbookBom.eq(WmPurchaseTaskbookBom::getWmTaskbookId, taskbook.getId())
                        .eq(WmPurchaseTaskbookBom::getWmTaskId, taskRoot.getId());
                List<WmPurchaseTaskbookBom> wmPurchaseTaskbookBomList = wmPurchaseTaskbookBomService.list(queryWrapperTaskbookBom);
                for(WmPurchaseTaskbookBom wmPurchaseTaskbookBom: wmPurchaseTaskbookBomList){
                    //只要一个未确认（空值），都返回未确认
                    if(
                        StringUtils.isEmpty(wmPurchaseTaskbookBom.getIsContainAssistMaterial())
                        || StringUtils.isEmpty(wmPurchaseTaskbookBom.getIsContainDebug())
                        || StringUtils.isEmpty(wmPurchaseTaskbookBom.getIsContainControl())
                        || StringUtils.isEmpty(wmPurchaseTaskbookBom.getIsContainInstall())
                        || StringUtils.isEmpty(wmPurchaseTaskbookBom.getIsContainMainMaterial())
                        || StringUtils.isEmpty(wmPurchaseTaskbookBom.getIsContainElectric())
                    ){
                        taskRoot.setIsExtraConfirm(false);
                        break;
                    }
                }
            }

            //7、合同产品
            WmContractProduct wmContractProduct = wmContractProductService.getById(taskbook.getWmContractProductId());
            taskbook.setWmContractProduct(wmContractProduct);

            Boolean isSendUnitSetting = wmTaskService.produceTaskHasSendUnit(taskbook.getId());
            taskbook.setIsSendUnitSetting(isSendUnitSetting);

            //技改
            WmSkillChange wmSkillChange = wmSkillChangeService.queryLatestRow(taskbook.getId());
            if(wmSkillChange != null && wmSkillChange.getStatus().equals(UserConstant.TASK_STATUS_PREPARE)){
                taskbook.setCanTransferSkill(true);
            }
            wmTaskbookListNew.add(taskbook);
        }

        return wmTaskbookListNew;
    };

    /**
     * 递归获取任务子任务列表
     * @param wmTaskParent
     * @return
     */
    private List<WmTask> queryTaskChildrenUntilStartPoint(WmTask wmTaskParent){
        List<WmTask> wmTaskListFinal = new ArrayList<>();
        //if(wmTaskParent.getIsStartPoint().equals(UserConstant.SYS_NO)) {
            //循环到设计起点任务时，下面子任务不再往下获取（已取消）
            LambdaQueryWrapper<WmTask> queryWrapperTask = new LambdaQueryWrapper<>();
            queryWrapperTask.eq(WmTask::getPid, wmTaskParent.getId());
            if(wmTaskParent.getCurrentVersion() == null){
                queryWrapperTask.isNull(WmTask::getCurrentVersion);
            }
            else{
                queryWrapperTask.eq(WmTask::getCurrentVersion, wmTaskParent.getCurrentVersion());
            }

            List<WmTask> wmTaskList = wmTaskService.list(queryWrapperTask);
            for (WmTask wmTask : wmTaskList) {
                wmTask.setTaskbookStatus(wmTaskParent.getTaskbookStatus());
                wmTask.setWmTaskbookFlowId(wmTaskParent.getWmTaskbookFlowId());
                List<WmTask> taskChildren = queryTaskChildrenUntilStartPoint(wmTask);
                wmTask.setChildren(taskChildren);

                List<WmTaskFlow> wmTaskFlowList = wmTaskFlowService.queryByTaskId(wmTask.getId());
                wmTask.setWmTaskFlowList(wmTaskFlowList);
                if(!StringUtils.isEmpty(wmTask.getCurrentNodeId())) {
                    SysDepartModel sysDepartModel = sysBaseApi.selectAllById(wmTask.getCurrentNodeId());
                    wmTask.setCurrentNodeId_dictText(sysDepartModel.getDepartName());
                }
                wmTaskListFinal.add(wmTask);
            }
        //}
        return wmTaskListFinal;
    }

    @Override
    public WmTaskbook selectTaskbookWithTask(WmTaskbook wmTaskbook){
        //1、通过任务书ID获取任务
        LambdaQueryWrapper<WmTask> queryWrapperTask = new LambdaQueryWrapper<>();
        queryWrapperTask.eq(WmTask::getWmTaskbookId, wmTaskbook.getId());
        queryWrapperTask.eq(WmTask::getWmContractProductId, wmTaskbook.getWmContractProductId());
        if(wmTaskbook.getCurrentVersion() == null){
            queryWrapperTask.isNull(WmTask::getCurrentVersion);
        }
        else{
            queryWrapperTask.eq(WmTask::getCurrentVersion, wmTaskbook.getCurrentVersion());
        }
        queryWrapperTask.last("LIMIT 1");
        WmTask taskRoot = wmTaskService.getOne(queryWrapperTask);
        if(taskRoot != null) {
            taskRoot.setTaskbookStatus(wmTaskbook.getStatus());
            List<WmTask> wmTaskListChildren = queryTaskChildren(taskRoot);
            taskRoot.setChildren(wmTaskListChildren);
            if(!StringUtils.isEmpty(taskRoot.getCurrentNodeId())) {
                SysDepartModel sysDepartModel = sysBaseApi.selectAllById(taskRoot.getCurrentNodeId());
                taskRoot.setCurrentNodeId_dictText(sysDepartModel.getDepartName());
            }

            //2、获取任务流程时间明细
            List<WmTaskFlow> wmTaskFlowList = wmTaskFlowService.queryByTaskId(taskRoot.getId());
            taskRoot.setWmTaskFlowList(wmTaskFlowList);

            //3、任务列表添加到任务书
            List<WmTask> wmTaskList = new ArrayList<>();
            wmTaskList.add(taskRoot);
            wmTaskbook.setChildren(wmTaskList);

            //4、判断能否转换为生产任务书
            if (wmTaskbook.getTaskbookType().equals(UserConstant.TASKBOOK_TYPE_DESIGN) && wmTaskbook.getIsTransferProduceTaskbook().equals(UserConstant.SYS_NO)) {
                //是否有生产任务，且没有转换过，则可以转换成生产任务书
                LambdaQueryWrapper<WmTask> queryWrapperTaskProduce = new LambdaQueryWrapper<>();
                queryWrapperTaskProduce.eq(WmTask::getWmTaskbookId, wmTaskbook.getId());
                queryWrapperTaskProduce.ne(WmTask::getId, taskRoot.getId());
                queryWrapperTaskProduce.eq(WmTask::getType, UserConstant.TASK_TYPE_SELF);
                if(wmTaskbook.getCurrentVersion() == null){
                    queryWrapperTask.isNull(WmTask::getCurrentVersion);
                }
                else{
                    queryWrapperTask.eq(WmTask::getCurrentVersion, wmTaskbook.getCurrentVersion());
                }
                queryWrapperTaskProduce.last("LIMIT 1");
                WmTask taskProduce = wmTaskService.getOne(queryWrapperTaskProduce);
                if (taskProduce != null) {
                    wmTaskbook.setCanTransferProduce(true);
                }
            }

            //5、判断能否转换为采购任务书
            if(wmTaskbook.getTaskbookType().equals(UserConstant.TASKBOOK_TYPE_PROCESS) && wmTaskbook.getIsTransferPurchaseTaskbook().equals(UserConstant.SYS_NO) && wmTaskbook.getStatus().equals(UserConstant.TASK_STATUS_DESIGN_APPROVED)){
                wmTaskbook.setCanTransferPurchase(true);
            }
        }

        return wmTaskbook;
    }

    /**
     * 递归获取任务子任务列表
     * @param wmTaskParent
     * @return
     */
    private List<WmTask> queryTaskChildren(WmTask wmTaskParent){
        List<WmTask> wmTaskListFinal = new ArrayList<>();
        //循环到发货单位时，下面子任务不再往下获取
        LambdaQueryWrapper<WmTask> queryWrapperTask = new LambdaQueryWrapper<>();
        queryWrapperTask.eq(WmTask::getPid, wmTaskParent.getId());
        if(wmTaskParent.getCurrentVersion() == null){
            queryWrapperTask.isNull(WmTask::getCurrentVersion);
        }
        else{
            queryWrapperTask.eq(WmTask::getCurrentVersion, wmTaskParent.getCurrentVersion());
        }

        List<WmTask> wmTaskList = wmTaskService.list(queryWrapperTask);
        for (WmTask wmTask : wmTaskList) {
            if(!StringUtils.isEmpty(wmTask.getCurrentNodeId())) {
                SysDepartModel sysDepartModel = sysBaseApi.selectAllById(wmTask.getCurrentNodeId());
                wmTask.setCurrentNodeId_dictText(sysDepartModel.getDepartName());
            }

            wmTask.setTaskbookStatus(wmTaskParent.getTaskbookStatus());
            List<WmTask> taskChildren = queryTaskChildren(wmTask);
            wmTask.setChildren(taskChildren);
            List<WmTaskFlow> wmTaskFlowList = wmTaskFlowService.queryByTaskId(wmTask.getId());
            wmTask.setWmTaskFlowList(wmTaskFlowList);
            wmTaskListFinal.add(wmTask);
        }
        return wmTaskListFinal;
    }

    @Override
    public List<String> getExpandKeys(WmTaskbook wmTaskbook){
        List<String> keyList = new ArrayList<>();
        keyList.add(wmTaskbook.getId());

        //1、通过任务书ID获取任务
        LambdaQueryWrapper<WmTask> queryWrapperTask = new LambdaQueryWrapper<>();
        queryWrapperTask.eq(WmTask::getWmTaskbookId, wmTaskbook.getId());
        queryWrapperTask.eq(WmTask::getWmContractProductId, wmTaskbook.getWmContractProductId());
        if(wmTaskbook.getCurrentVersion() == null){
            queryWrapperTask.isNull(WmTask::getCurrentVersion);
        }
        else{
            queryWrapperTask.eq(WmTask::getCurrentVersion, wmTaskbook.getCurrentVersion());
        }
        queryWrapperTask.last("LIMIT 1");
        WmTask taskRoot = wmTaskService.getOne(queryWrapperTask);
        if(taskRoot != null) {
            keyList.add(taskRoot.getId());

            List<String> wmTaskIdListChildren = queryTaskChildrenIdUntilSendUnit(taskRoot);
            if (!wmTaskIdListChildren.isEmpty()) {
                keyList.addAll(wmTaskIdListChildren);
            }
        }

        return keyList;
    };

    /**
     * 递归获取任务子任务列表
     * @param wmTaskParent
     * @return
     */
    private List<String> queryTaskChildrenIdUntilSendUnit(WmTask wmTaskParent){
        List<String> wmTaskIdListFinal = new ArrayList<>();
        if(wmTaskParent.getIsSendUnit().equals(UserConstant.SYS_NO)) {
            //循环到发货单位时，下面子任务不再往下获取
            LambdaQueryWrapper<WmTask> queryWrapperTask = new LambdaQueryWrapper<>();
            queryWrapperTask.eq(WmTask::getPid, wmTaskParent.getId());
            if(wmTaskParent.getCurrentVersion() == null){
                queryWrapperTask.isNull(WmTask::getCurrentVersion);
            }
            else{
                queryWrapperTask.eq(WmTask::getCurrentVersion, wmTaskParent.getCurrentVersion());
            }
            List<WmTask> wmTaskList = wmTaskService.list(queryWrapperTask);
            for (WmTask wmTask : wmTaskList) {
                if(wmTask.getIsSendUnit().equals(UserConstant.SYS_NO)) {
                    wmTaskIdListFinal.add(wmTask.getId());
                }
                List<String> taskChildrenId = queryTaskChildrenIdUntilSendUnit(wmTask);
                if(!taskChildrenId.isEmpty()){
                    wmTaskIdListFinal.addAll(taskChildrenId);
                }
            }
        }
        return wmTaskIdListFinal;
    }

    @Override
    public void saveTaskbookAndTask(WmTaskbook wmTaskbook, WmContractProduct wmContractProduct){
        //任务书流程和流程列表
        WmTaskbookFlow wmTaskbookFlow = wmTaskbookFlowService.getById(wmTaskbook.getWmTaskbookFlowId());
        List<WmFlowDept> wmFlowDeptList = wmFlowDeptService.selectByMainId(wmTaskbook.getWmTaskbookFlowId());
        wmTaskbook.setTaskbookType(wmTaskbookFlow.getTaskbookType());
        wmTaskbook.setCurrentNodeId(wmFlowDeptList.get(0).getDeptId());
        wmTaskbook.setCurrentNodeIndex(0);
        if(StringUtils.isEmpty(wmTaskbook.getWeight())){
            wmTaskbook.setWeight("1");
        }

        //1、保存任务书
        if (StringUtils.isEmpty(wmTaskbook.getCode())) {
            String taskbookCode = (String) FillRuleUtil.executeRule(FillRuleConstant.PLAN, null);
            wmTaskbook.setCode(taskbookCode);
        }
        wmTaskbook.setWmContractId(wmContractProduct.getWmContractId());
        this.save(wmTaskbook);

        //2、保存任务书任务
        WmTask wmTask = new WmTask();
        wmTask.setWmContractProductId(wmTaskbook.getWmContractProductId());
        wmTask.setWmTaskbookId(wmTaskbook.getId());
        wmTask.setName(wmContractProduct.getName());
        wmTask.setCode(wmContractProduct.getCode());
        wmTask.setWmItemId(wmContractProduct.getWmItemId());
        wmTask.setQuantity(wmContractProduct.getQuantity());
        wmTask.setSpecification(wmContractProduct.getSpecification());
        wmTask.setUnitOfMeasure(wmContractProduct.getUnitOfMeasure());
        wmTask.setItemOrProduct(wmContractProduct.getItemOrProduct());
        wmTask.setWeight(wmTaskbook.getWeight());
        wmTask.setChargeBy(wmTaskbook.getChargeBy());
        wmTask.setPlanWorkHour(wmTaskbook.getPlanWorkHour());
        wmTask.setCurrentNodeId(wmFlowDeptList.get(0).getDeptId());
        wmTask.setCurrentNodeIndex(0);
        wmTask.setCurrentNodeTime(new Date());
        wmTask.setCanEditFlow(UserConstant.SYS_YES);
        wmTask.setEnableFlag(UserConstant.SYS_YES);
        wmTask.setBuId(wmTaskbook.getBuId());
        wmTask.setTaskbookType(wmTaskbook.getTaskbookType());
        wmTask.setPlanBeginTime(wmTaskbook.getPlanBeginTime());
        wmTask.setPlanEndTime(wmTaskbook.getPlanEndTime());
        if(wmTaskbook.getTaskbookType().equals(UserConstant.TASKBOOK_TYPE_DESIGN)){
            //类型设计任务书根任务不为设计起点任务
            wmTask.setIsStartPoint(UserConstant.SYS_NO);
        }
        else{
            //类型为技术服务任务书、采购任务书、工艺任务书、生产任务书、安装调试任务书的时候，根任务为设计起点任务
            wmTask.setIsStartPoint(UserConstant.SYS_YES);
        }

        //生产任务书类型
        List<String> produceTypeList = new ArrayList<>();
        produceTypeList.add(UserConstant.TASKBOOK_TYPE_PRODUCE);
        produceTypeList.add(UserConstant.TASKBOOK_TYPE_PRODUCE_ELECTRIC);
        if(produceTypeList.contains(wmTaskbook.getTaskbookType())){
            //类型为生产任务书的时候，根任务为发货单位(当新建生产任务书时)
            wmTask.setIsSendUnit(UserConstant.SYS_YES);
        }
        wmTaskService.addWmTask(wmTask);

        //3、保存任务流程
        for(WmFlowDept wmFlowDept: wmFlowDeptList){
            WmTaskFlow wmTaskFlow = new WmTaskFlow();
            wmTaskFlow.setDepartId(wmFlowDept.getDeptId());
            wmTaskFlow.setResponsibility(wmFlowDept.getResponsibility());
            wmTaskFlow.setWmTaskId(wmTask.getId());
            wmTaskFlow.setSysOrgCode(wmFlowDept.getSysOrgCode());
            wmTaskFlow.setOrderNum(wmFlowDept.getOrderNum());
            //修改任务部门流程，如果前端有传设置信息
            if(!CollectionUtils.isEmpty(wmTaskbook.getWmTaskFlowList())) {
                Integer index = 0;
                for(WmTaskFlow wmTaskFlowItem: wmTaskbook.getWmTaskFlowList()){
                    if(wmTaskFlowItem.getDepartId().equals(wmFlowDept.getDeptId()) && index.equals(wmFlowDept.getOrderNum())) {
                        wmTaskFlow.setResponsibility(wmTaskFlowItem.getResponsibility());
                        wmTaskFlow.setChargeBy(wmTaskFlowItem.getChargeBy());
                        wmTaskFlow.setFinishTime(wmTaskFlowItem.getFinishTime());
                        break;
                    }
                    index++;
                }
            }
            wmTaskFlowService.save(wmTaskFlow);
        }

        //4、初始化任务日志（新建任务书时）
        wmTaskLogService.initTaskLog(wmTask);
    };

    @Override
    public List<String> selectIdListByContractIdList(List<String> wmContractIdList){
        List<String> idList = new ArrayList<>();
        LambdaQueryWrapper<WmTaskbook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(WmTaskbook::getWmContractId, wmContractIdList);
        List<WmTaskbook> wmTaskbookList = list(queryWrapper);
        for(WmTaskbook wmTaskbook: wmTaskbookList){
            idList.add(wmTaskbook.getId());
        }

        return idList;
    };

    @Override
    public WmTaskbook getByBatchNo(String batchNo){
        LambdaQueryWrapper<WmTaskbook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmTaskbook::getBatchNo, batchNo).last("LIMIT 1");
        WmTaskbook wmTaskbook = getOne(queryWrapper);
        return wmTaskbook;
    };

    @Override
    public List<WmTaskbook> selectByContractProduct(List<String> wmContractProductList){
        LambdaQueryWrapper<WmTaskbook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(WmTaskbook::getWmContractProductId, wmContractProductList);
        List<WmTaskbook> wmTaskbookList = list(queryWrapper);
        return wmTaskbookList;
    };

    @Override
    public List<WmTaskbook> searchByName(String searchKey){
        LambdaQueryWrapper<WmTaskbook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(WmTaskbook::getName, searchKey);
        return list(queryWrapper);
    };

    @Override
    public List<WmTaskbook> selectByContractProduct(String wmContractProductId){
        LambdaQueryWrapper<WmTaskbook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmTaskbook::getWmContractProductId, wmContractProductId);
        List<WmTaskbook> wmTaskbookList = list(queryWrapper);
        return wmTaskbookList;
    };

    @Override
    public WmTaskbook selectProduceTaskbookByDesignTaskbook(WmTaskbook wmTaskbookDesign){
        LambdaQueryWrapper<WmTaskbook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmTaskbook::getWmTaskbookId, wmTaskbookDesign.getId())
                .orderByDesc(WmTaskbook::getCreateTime)
                .last("LIMIT 1");
        WmTaskbook wmTaskbook = getOne(queryWrapper);
        return wmTaskbook;
    };
}
